package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.DailyTrainSeat;
import com.system.business.domain.TrainSeat;
import com.system.business.domain.TrainStation;
import com.system.business.req.DailyTrainSeatQueryReq;
import com.system.business.req.DailyTrainSeatSaveReq;
import com.system.business.resp.DailyTrainSeatQueryResp;
import com.system.business.service.DailyTrainSeatService;
import com.system.business.mapper.DailyTrainSeatMapper;
import com.system.business.service.TrainSeatService;
import com.system.business.service.TrainStationService;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author Admin
* @description 针对表【daily_train_seat(每日座位)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:15
*/
@Service
public class DailyTrainSeatServiceImpl extends ServiceImpl<DailyTrainSeatMapper, DailyTrainSeat>
    implements DailyTrainSeatService{
    @Autowired
    private DailyTrainSeatMapper dailyTrainSeatMapper;
    @Autowired
    private TrainSeatService trainSeatService;
    @Autowired
    private TrainStationService trainStationService;
    @Override
    public void save(DailyTrainSeatSaveReq req) {
        DateTime now = DateTime.now();

        DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(req, DailyTrainSeat.class);
        if (ObjectUtil.isNull(dailyTrainSeat.getId())) {
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        } else {
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.updateById(dailyTrainSeat);
        }
    }

    @Override
    public PageResp<DailyTrainSeatQueryResp> queryList(DailyTrainSeatQueryReq req) {
        Page<DailyTrainSeat> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<DailyTrainSeat> passengerQueryWrapper = new QueryWrapper<>();
        Page<DailyTrainSeat> pageData = dailyTrainSeatMapper.selectPage(pageCondition,passengerQueryWrapper);
        List<DailyTrainSeatQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), DailyTrainSeatQueryResp.class);

        PageResp<DailyTrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Transactional
    @Override
    public void genDaily(Date date, String trainCode) {
        // 删除某日某车次的座位信息
        QueryWrapper<DailyTrainSeat> dailyTrainSeatQueryWrapper = new QueryWrapper<>();
        dailyTrainSeatQueryWrapper.eq("train_code",trainCode).eq("date",date);
        dailyTrainSeatMapper.delete(dailyTrainSeatQueryWrapper);

        QueryWrapper<TrainSeat> trainSeatQueryWrapper = new QueryWrapper<>();
        trainSeatQueryWrapper.eq("train_code",trainCode);
        List<TrainSeat> seatList = trainSeatService.list(trainSeatQueryWrapper);


        List<TrainStation> stationList = trainStationService.selectByTrainCode(trainCode);
        String sell = StrUtil.fillBefore("", '0', stationList.size() - 1);
        // 查出某车次的所有的座位信息
        if (CollUtil.isEmpty(seatList)) {
            return;
        }
        for (TrainSeat trainSeat : seatList) {
            DateTime now = DateTime.now();
            DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(trainSeat, DailyTrainSeat.class);
            dailyTrainSeat.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeat.setDate(date);
            dailyTrainSeat.setSell(sell);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        }
    }

    @Override
    public int countSeat(Date date, String trainCode, String code) {
        QueryWrapper<DailyTrainSeat> dailyTrainSeatQueryWrapper = new QueryWrapper<>();
        dailyTrainSeatQueryWrapper.eq("date",date)
                .eq("train_code",trainCode)
                .eq("seat_type",code);
        Long l = dailyTrainSeatMapper.selectCount(dailyTrainSeatQueryWrapper);
        if(l == 0){
            return -1;
        }
        return Math.toIntExact(l);
    }

    @Override
    public List<DailyTrainSeat> selectByCarriage(Date date, String trainCode, Integer indexCode) {
        QueryWrapper<DailyTrainSeat> dailyTrainSeatQueryWrapper = new QueryWrapper<>();
        dailyTrainSeatQueryWrapper.eq("date",date)
                .eq("train_code",trainCode).eq("carriage_index",indexCode)
                .orderByAsc("carriage_seat_index");
        return dailyTrainSeatMapper.selectList(dailyTrainSeatQueryWrapper);
    }

    @Override
    public DailyTrainSeat selectByNotSell(Date date, String trainCode, String seatType) {
        QueryWrapper<DailyTrainSeat> dailyTrainSeatQueryWrapper = new QueryWrapper<>();
        dailyTrainSeatQueryWrapper.eq("date",date)
                .eq("train_code",trainCode).eq("sell",0)
                .eq("seat_type",seatType);
        return dailyTrainSeatMapper.selectList(dailyTrainSeatQueryWrapper).get(0);
    }
}
